<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Tags_private extends wsFrontend
{

    public function __construct()
    {
        parent::__construct();
        if (strpos(strtolower($this->uri->uri_string()), 'tags_private') !== false) {
            redirect();
        }
        $this->db->cache_set_path(dirname($this->db->cachedir) . '/tags_page/');
    }

    public function index()
    {
        $data = array();

        $data['url'] = $this->route_links->get('tags_index_page');
        $this->load->model(array('wallpaper_model', 'category_model'));


        $filter['per_page'] = $config['per_page'] = $this->config->item('items_per_page') * 3;
        $page_number = (int)$this->input->get('page');
        $filter['offset'] = $data['offset'] = ($page_number ? ($page_number - 1) * $config['per_page'] : 0);

        $data['total_count'] = $this->wallpaper_model->get_tags($filter + array('count' => true));
        $data['top_count'] = $this->wallpaper_model->get_tags($filter + array('top_count' => true)) * 1.3;

        //Initialize Pagination
        $config['base_url'] = $data['url'];
        $config['total_rows'] = $data['total_count'];
        $config['custom_query_string'] = TRUE;
        $config['page_query_string'] = TRUE;
        $config['query_string_segment'] = 'page';

        if (!empty($_GET)) {
            $get_method = $this->frontend_helper->get_query_string(array('page'));
            $config['prefix'] = ($get_method ? $get_method . '&' : '?') . 'page=';
            $config['first_url'] = $config['base_url'] . $get_method;
        } else {
            $config['prefix'] = '?page=';
        }

        $this->load->library('pagination');
        $this->pagination->initialize($config);
        $data['pagination'] = $this->pagination->create_links();
        $data['tags'] = $this->wallpaper_model->get_tags($filter);


        $this->tpl->breadcrumbs['壁纸标签'] = 'active';


        $this->tpl->meta ['title'] = $this->metadata->get_metadata('_tag_index_title', 'tags_page', false, $page_number);
        $this->tpl->meta ['description'] = $this->metadata->get_metadata('_tag_index_description', 'tags_page', false, $page_number);
        $this->tpl->meta ['keywords'] = $this->metadata->get_metadata('_tag_index_keywords', 'tags_page', false, $page_number);


        $this->tpl->contents [] = $this->tpl->fetch('contents/tag_index_page', $data);
        $this->tpl->render('layouts/home');

    }

    public function get($tag = '')
    {
        if (!$tag)
            redirect($this->route_links->get('tags_index'));

        $data['url'] = current_url();
        $this->load->model(array('wallpaper_model', 'category_model'));

        $filter['show_disabled'] = FALSE;
        $filter['per_page'] = $config['per_page'] = $this->config->item('items_per_page');
        $page_number = (int)$this->input->get('page');
        $filter['offset'] = ($page_number ? ($page_number - 1) * $config['per_page'] : 0);
        $filter['bypath'] = false;
        $filter['tag'] = $data['tag'] = url_title($tag, true, '-');


        if (!empty($_GET['sort']))
            $filter['sort'] = $_GET['sort'];

        if (isset($_GET['time']))
            $filter['time'] = $_GET['time'];


        $data['total_count'] = $this->wallpaper_model->getWallpapers($filter + array('count' => true));

        //Initialize Pagination
        $config['base_url'] = $data['url'];
        $config['total_rows'] = $data['total_count'];
        $config['custom_query_string'] = TRUE;
        $config['page_query_string'] = TRUE;
        $config['query_string_segment'] = 'page';

        if (!empty($_GET)) {
            $get_method = $this->frontend_helper->get_query_string(array('page'));
            $config['prefix'] = ($get_method ? $get_method . '&' : '?') . 'page=';
            $config['first_url'] = $config['base_url'] . $get_method;
        } else {
            $config['prefix'] = '?page=';
        }

        $this->load->library('pagination');
        $this->pagination->initialize($config);
        $data['pagination'] = $this->pagination->create_links();
        $data['wallpapers'] = $this->wallpaper_model->getWallpapers($filter);


        $this->tpl->breadcrumbs['Tags'] = $this->route_links->build_link('tags_index_page');
        $this->tpl->breadcrumbs[$data['tag']] = 'active';

        $SearchReplace = array(
            '{tag}' => $data['tag']
        );

        $this->tpl->meta ['title'] = $this->metadata->get_metadata('_tags_view_title', 'tags_page', $SearchReplace, false, $page_number);
        $this->tpl->meta ['description'] = $this->metadata->get_metadata('_tags_view_description', 'tags_page', $SearchReplace, false, $page_number);
        $this->tpl->meta ['keywords'] = $this->metadata->get_metadata('_tags_view_keywords', 'tags_page', $SearchReplace, false, $page_number);


        $this->tpl->contents [] = $this->tpl->fetch('contents/tag_view_page', $data);
        $this->tpl->render('layouts/home');
    }

}